package cc.web.vaaking.servlet.vacation;

import cc.web.vaaking.common.AjaxResult;
import cc.web.vaaking.common.AjaxTool;
import cc.web.vaaking.common.LoginContext;
import cc.web.vaaking.common.LoginUser;
import cc.web.vaaking.common.core.StateMachine;
import cc.web.vaaking.common.utils.ValueUtils;
import cc.web.vaaking.entity.SysVacationRecord;
import cc.web.vaaking.services.SysVacationRecordService;
import cc.web.vaaking.servlet.BaseHttpServlet;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/approve/*")
public class ApproveServlet extends BaseHttpServlet {
    SysVacationRecordService sysVacationRecordService = new SysVacationRecordService();
    @Override
    public void init() throws ServletException {
        routerMap.put("mod",this::mod); //修改
        routerMap.put("pass",this::pass); //通过
        routerMap.put("reject",this::reject); // 拒绝
        routerMap.put("undo",this::undo);//撤销
        routerMap.put("finish",this::finish);//销假
    }

    private void finish(HttpServletRequest request, HttpServletResponse response) {
        try {
            SysVacationRecord sysVacationRecord = AjaxTool.getJSON(request).toJavaObject(SysVacationRecord.class);
            sysVacationRecord.setStatus("9");
            sysVacationRecordService.updateByPrimaryKeySelective(sysVacationRecord);
            long days = DateUtil.between(sysVacationRecord.getStartTime(),DateUtil.date(), DateUnit.DAY);
            String tips = StrUtil.format("操作成功,您本次休假{}天",days);
            AjaxTool.returnAjax(response, AjaxResult.success(tips));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxTool.returnAjax(response, AjaxResult.error("失败"));
        }
    }

    private void undo(HttpServletRequest request, HttpServletResponse response) {
        try {
            SysVacationRecord sysVacationRecord = AjaxTool.getJSON(request).toJavaObject(SysVacationRecord.class);
            sysVacationRecord.setStatus("4");
            sysVacationRecordService.updateByPrimaryKeySelective(sysVacationRecord);
            AjaxTool.returnAjax(response, AjaxResult.success("操作成功"));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxTool.returnAjax(response, AjaxResult.error("失败"));
        }
    }

    private void mod(HttpServletRequest request, HttpServletResponse response) {
    }

    private void reject(HttpServletRequest request, HttpServletResponse response) {
        try {
            SysVacationRecord sysVacationRecord = AjaxTool.getJSON(request).toJavaObject(SysVacationRecord.class);
            StateMachine.goNext(sysVacationRecord, false, LoginContext.getCurrentUser().getRoleId());
            AjaxTool.returnAjax(response, AjaxResult.success("操作成功"));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxTool.returnAjax(response, AjaxResult.error("失败"));
        }
    }

    private void pass(HttpServletRequest request, HttpServletResponse response) {
        try {
            SysVacationRecord sysVacationRecord = AjaxTool.getJSON(request).toJavaObject(SysVacationRecord.class);
            StateMachine.goNext(sysVacationRecord, true, LoginContext.getCurrentUser().getRoleId());
            AjaxTool.returnAjax(response, AjaxResult.success("操作成功"));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxTool.returnAjax(response, AjaxResult.error("失败"));
        }
    }
}
